package com.js.myapp.system.activity;

// Created by JS on 2021/1/18.

import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.Nullable;

import com.js.basicpage.activity.BaseLoadingDialogActivity;
import com.js.basicutils.NavigationBarUtil;
import com.js.basicutils.permission.PermissionConstant;
import com.js.basicutils.permission.PermissionListener;
import com.js.basicutils.permission.PermissionUtils;
import com.js.myapp.R;
import com.js.myapp.system.timer.SplashCountDownTimer;

import java.util.List;

public class SplashActivity extends BaseLoadingDialogActivity implements View.OnClickListener {

    protected ImageView ivBg;
    protected LinearLayout llJump;
    protected TextView tvSecond;
    protected SplashActivity mContext;

    private boolean isDone;
    private SplashCountDownTimer mCountDownTimer;
    private long delayTime = 4000; // 延时时间
    private long interval = 1000; // 间隔时间

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        mContext = this;
        setContentView(R.layout.activity_splash);
        NavigationBarUtil.hideBottomUI(getWindow().getDecorView());
        initView();
    }

    protected void initView() {
        ivBg = findViewById(R.id.iv_bg);
        llJump = findViewById(R.id.ll_jump);
        tvSecond = findViewById(R.id.tv_second);
        ivBg.setImageResource(R.mipmap.pic_splash);
        llJump.setOnClickListener(this);
        llJump.setVisibility(View.GONE);
        checkPermission();
    }

    /**
     * 检查权限
     */
    private void checkPermission() {
        String[] permissions = {
                PermissionConstant.WRITE_EXTERNAL_STORAGE,
                PermissionConstant.READ_EXTERNAL_STORAGE,
                PermissionConstant.FINE_LOCATION,
                PermissionConstant.COARSE_LOCATION,
                PermissionConstant.CAMERA,
                PermissionConstant.CALL_PHONE,
                PermissionConstant.READ_PHONE_STATE
        };
        if (!PermissionUtils.hasPermissions(this, permissions)) {
            PermissionUtils.getPermission(this, permissions, new PermissionListener() {
                @Override
                public void onGranted() {
                    doNextDelay();
                }

                @Override
                public void onDenied(List<String> deniedPermissions, boolean neverTips) {
                    PermissionUtils.showPermissionSettingDialog(SplashActivity.this, getPackageName());
                }
            });
        } else {
            doNextDelay();
        }
    }

    /**
     * 延时执行下一步
     */
    private void doNextDelay() {
        llJump.setVisibility(View.VISIBLE);
        tvSecond.setText("3s");
        mCountDownTimer = new SplashCountDownTimer(delayTime, interval, tvSecond);
        mCountDownTimer.start();
        new Handler().postDelayed(() -> {
            if (!isDone) doNext();
        }, delayTime);
    }

    /**
     * 执行下一步
     */
    private void doNext() {
        isDone = true;
        MainActivity.start(mContext);
        finish();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.ll_jump:
                doNext();
                break;
        }
    }

    @Override
    protected void onDestroy() {
        if (mCountDownTimer != null) mCountDownTimer.cancel();
        super.onDestroy();
    }
}
